/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ia_multiagent1;

import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Harakan
 */
public class Espace2D {
    
    private int dimGrille = 50;
    private int nbRessources = 20;
    private Case [][] E2D;
    private Vector <PointAttraction> mesPointsAttraction;
    private int sensibilitePheromoneToBeAPA = 10;

    public Vector<PointAttraction> getMesPointsAttraction() {
        return mesPointsAttraction;
    }

    public void setMesPointsAttraction(Vector<PointAttraction> mesPointsAttraction) {
        this.mesPointsAttraction = mesPointsAttraction;
    }
    
    
    
    public Espace2D (int dimGrille,int nbRessources){
        this.dimGrille = dimGrille;
        this.nbRessources = nbRessources;
        E2D = new Case[dimGrille][dimGrille];
        
        for(int i = 0; i < dimGrille; i++){
             for(int j = 0; j < dimGrille; j++){
                 setCase(i,j);
             }
         }
         mesPointsAttraction = new Vector <PointAttraction>();
    }
       
    /* récuperer la case ligne i, colonne j 
     * 
     */
    public Case getCase(int i, int j){
        return E2D[j][i];
    }

   
    
    public void setCase(int i, int j){
        E2D[j][i] = new Case(i,j);
    }
    
    public void setCase(int i, int j, int ressource){
        Case maCase = new Case(i,j);
        maCase.setRessource(ressource);
        E2D[j][i] = maCase;       
    }
    
    public void setCase(int i, int j, int ressource, int life){
        Case maCase = this.getCase(i, j);
        maCase.setRessource(ressource);
        maCase.setLifeRessource(life);    
    }
    

    
    public void print(){
         for(int i = 0; i < dimGrille; i++){
             System.out.print("\n");
             for(int j = 0; j < dimGrille; j++){
                 getCase(i,j).print();
                 System.out.print(" ");
             }
         }
         System.out.print("\n");
    }

    
    public boolean estVide(int x, int y){
        return getCase(x, y).estVideDeRessources();
    }
    
        public int getDimGrille() {
        return dimGrille;
    }

        
        
    public void setChampAttractifDroite() {
   //      for(int i = 0; i < dimGrille; i++){
             for(int j = 0; j < dimGrille; j++){
//  OLD               E2D[j][i].setAttraction(i);
                 PointAttraction PA = new PointAttraction(49, j,20 , 10000);
                 mesPointsAttraction.add(PA);                 
             }
   //      }         
    }
    
        public void setChampAttractifDroite(int portee, int force) {
   //      for(int i = 0; i < dimGrille; i++){
             for(int j = 0; j < dimGrille; j++){
//  OLD               E2D[j][i].setAttraction(i);
                 PointAttraction PA = new PointAttraction(49, j, portee , force);
                 mesPointsAttraction.add(PA);
             }
   //      }         
    }
    
        public String mesPointsAttractionToString() {
        String res = "PA numero ";
        for (int i=0;i<mesPointsAttraction.size();i++){
            res = res + i + "x = "+ mesPointsAttraction.elementAt(i).getX() +
                          ", y = "+ mesPointsAttraction.elementAt(i).getY()+
                          " a une intensite = "+mesPointsAttraction.elementAt(i).getIntensite()+"\n";        
        }
        return res;
    }
        
        
        //ATTENTION. Cette fonction reinitialise tous les PA. 
        public Vector<PointAttraction> getMesPointsAttractionFonctionPheromones(){  
            mesPointsAttraction.removeAllElements();        
         //   System.out.println("les PAs sont reinitialises, size de mesPointsAttraction = " + mesPointsAttraction.size());
        
            Vector<PointAttraction> mesPA = new Vector<PointAttraction>();
           for(int i = 0; i < dimGrille; i++){
             for(int j = 0; j < dimGrille; j++){
              int nbPheromonesSurCetteCase = E2D[j][i].getQtePheromones();
              if (nbPheromonesSurCetteCase>=sensibilitePheromoneToBeAPA){
                 PointAttraction PA = new PointAttraction(49, j,nbPheromonesSurCetteCase , 100 * nbPheromonesSurCetteCase);
                 mesPointsAttraction.add(PA);  
            //     System.out.print("PA ajoute en i=" + i + " et j = "+ j + "\n");
                 
              }
             }
         }  
            //System.out.println("il y a en tout = " + mesPointsAttraction.size() + " PA actifs");        
            return mesPA;
        }
     
}


